<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用状态</title>
</head>

<body>
    <h1>state</h1>
    <div id="app"></div>

    <script src="../node_modules/react/umd/react.development.js"></script>
    <script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
    <script src="../node_modules/babel-standalone/babel.min.js"></script>
    <script type="text/babel">

        // 状态使用：我们通过类组件来学习
        class MyCom extends React.Component {

            // 在构造方法中，使用this.state = {}的方式，来初始化组件的状态
            constructor(props) {
                super(props);
                this.state = {
                    list: [],
                }
            }

            render() {
                // 类组件需要使用this来访问state
                return (
                    <div>
                        <div>
                            <input type="text" ref="myInput"/>
                            <button onClick={() => {
                                    const value = this.refs.myInput.value;
                                    if (value) {
                                        this.state.list.push(value);
                                        this.setState({list: this.state.list});
                                    }
                                }
                            }>提交</button>
                        </div>
                        <ol>
                            {this.state.list.map(v => <li key={v}>{v}</li>)}
                        </ol>
                    </div>
                )
            }

        }

        const app = document.querySelector("#app");
        const root = ReactDOM.createRoot(app);

        const dom = (
            <div>
                <MyCom />
            </div>)

        root.render(dom);
    </script>
</body>

</html>